package edu.xidian.bdilab.jrbigdata.service.bank.productApplyCheck.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.DealProduct;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseInfo;
import edu.xidian.bdilab.jrbigdata.domain.FinancialProduct;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.DealProductMapper;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseInfoMapper;
import edu.xidian.bdilab.jrbigdata.mapper.FinancialProductMapper;
import edu.xidian.bdilab.jrbigdata.mapper.ProductRequestMapper;
import edu.xidian.bdilab.jrbigdata.service.bank.productApplyCheck.ProductApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;


/**
 * @ClassName ProductApplyServiceImpl
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@Service
public class ProductApplyServiceImpl implements ProductApplyService {
    @Autowired
    FinancialProductMapper financialProductMapper;
    @Autowired
    ProductRequestMapper productRequestMapper;
    @Autowired
    DealProductMapper dealProductMapper;
    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;
    /**
     * 根据银行名称查询该银行所有的产品名称
     * @param bankName
     * @return
     */
    @Override
    public List<String> checkProName(String bankName) {
        QueryWrapper<FinancialProduct> financialProductQueryWrapper = new QueryWrapper<>();
        ArrayList<String> productName = new ArrayList<>();

        financialProductQueryWrapper.eq("bank_name",bankName);
        List<FinancialProduct> financialProductList = financialProductMapper.selectList(financialProductQueryWrapper);

        for (FinancialProduct financialProduct : financialProductList){
            productName.add(financialProduct.getProductName());
        }

        return productName;
    }
    /**
     * 根据一条产品名称查询相应的所有产品请求
     * @param productName
     * @return
     */
    @Override
    public List<ProductRequest> checkRroRequest(String productName) {
        QueryWrapper<ProductRequest> productRequestQueryWrapper = new QueryWrapper<>();
        productRequestQueryWrapper.eq("fk_product_name",productName);
        //productRequestQueryWrapper.eq("status",0);

        List<ProductRequest> productRequestList = productRequestMapper.selectList(productRequestQueryWrapper);

        return productRequestList;
    }
    /**
     * 查询银行所有金融产品的产品请求
     * @param
     * @return
     */
    public List<ProductRequest> getProRequest(String bankName){
        ArrayList<String> productName = (ArrayList<String>) this.checkProName(bankName);
        ArrayList<ProductRequest> productRequests = new ArrayList<>();

        for(String productname : productName){
            ArrayList<ProductRequest> nameList = (ArrayList<ProductRequest>) this.checkRroRequest(productname);
            for (ProductRequest productRequest : nameList){
                productRequests.add(productRequest);
            }
        }

        return productRequests;
    }

    /**
     * 根据一条产品名称查询相应的未处理产品请求
     * @param productName
     * @return
     */
    @Override
    public List<ProductRequest> checkRroRequestStatus0(String productName) {
        QueryWrapper<ProductRequest> productRequestQueryWrapper = new QueryWrapper<>();
        productRequestQueryWrapper.eq("fk_product_name",productName);
        productRequestQueryWrapper.eq("status",0);

        List<ProductRequest> productRequestList = productRequestMapper.selectList(productRequestQueryWrapper);

        return productRequestList;
    }


    /**
     * 查询银行所有金融产品的未处理产品请求
     * @param
     * @return
     */
    public List<ProductRequest> getProRequestStatus0(String bankName){
        ArrayList<String> productName = (ArrayList<String>) this.checkProName(bankName);
        ArrayList<ProductRequest> productRequests = new ArrayList<>();

        for(String productname : productName){
            ArrayList<ProductRequest> nameList = (ArrayList<ProductRequest>) this.checkRroRequestStatus0(productname);
            for (ProductRequest productRequest : nameList){
                productRequests.add(productRequest);
            }
        }

        return productRequests;
    }

    /**
     * 依据条件查看未处理企业申请
     * @param bankName
     * @return
     */
    @Override
    public List<ProductRequest> getProductApplyByRegionStatus0(String bankName,int region,String productName) {
        ArrayList<String> productNameList = new ArrayList<>();
        if (productName==null){
            productNameList = (ArrayList<String>) this.checkProName(bankName);
        }else {
            productNameList.add(productName);
        }
        if (region==-1){
            ArrayList<ProductRequest> productRequests = new ArrayList<>();
            for(String productname : productNameList){
                ArrayList<ProductRequest> allBankProRequestList = (ArrayList<ProductRequest>) this.checkRroRequestStatus0(productname);
                for (ProductRequest productRequest:allBankProRequestList) {
                    productRequests.add(productRequest);
                }
            }
            return productRequests;
        }else {
            ArrayList<ProductRequest> productRequests = new ArrayList<>();
            for(String productname : productNameList){
                ArrayList<ProductRequest> allBankProRequestList = (ArrayList<ProductRequest>) this.checkRroRequestStatus0(productname);
                for (ProductRequest productRequest : allBankProRequestList){
                    String enterpriseCode=productRequest.getFkSocialCode();
                    QueryWrapper<EnterpriseInfo> enterpriseInfoQueryWrapper = new QueryWrapper<>();
                    enterpriseInfoQueryWrapper.eq("social_code",enterpriseCode);
                    EnterpriseInfo enterprise=enterpriseInfoMapper.selectOne(enterpriseInfoQueryWrapper);
                    if (enterprise.getRegion()==region){
                        productRequests.add(productRequest);
                    }
                }
            }

            return productRequests;
        }
    }

    /*
     * 查看所有成交记录
     */
    @Override
    public ResponseResult getProductDeal(Integer pageNum,
                                         Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        try{
        List<DealProduct> dealProductList= dealProductMapper.selectList(null);

        PageInfo pageInfo=new PageInfo<> (dealProductList);
        ResponseResult responseResult = new ResponseResult();
        Map<String,Object> data=new HashMap<>(3);
        data.put("Deal List",dealProductList);
        data.put("Total Page Num",pageInfo.getPages());
        data.put("Total",pageInfo.getTotal());
        if (dealProductList.size() == 0) {
            responseResult.setCode(BankCode.CHECK_NONE.getCode());
            responseResult.setMessage(BankCode.CHECK_NONE.getMessage());
            responseResult.setSuccess(TRUE);
        } else {
            responseResult.setData(data);
            responseResult.setCode(BankCode.CHECK_SUCCESS.getCode());
            responseResult.setMessage(BankCode.CHECK_SUCCESS.getMessage());
            responseResult.setSuccess(TRUE);
        }
        return responseResult;
        }catch (Exception e){
            ResponseResult responseResult = new ResponseResult();
            responseResult.setCode(BankCode.CHENCK_FAILED.getCode());
            responseResult.setMessage(BankCode.CHENCK_FAILED.getMessage());
            responseResult.setSuccess(FALSE);
            return responseResult;
        }
    }


    /*
     * 企业申请成交
     */
    @Override
    public ResponseResult productDeal(int request_id) throws ParseException {
        ResponseResult responseResult = new ResponseResult();
        DealProduct dealProduct=new DealProduct();
        //根据id查出企业的产品申请
        QueryWrapper<ProductRequest> productRequestQueryWrapper = new QueryWrapper<>();
        productRequestQueryWrapper.eq("request_id",request_id);
        ProductRequest productRequest= productRequestMapper.selectOne(productRequestQueryWrapper);
        dealProduct.setSocialCode(productRequest.getFkSocialCode());
        dealProduct.setProductName(productRequest.getFkProductName());
        dealProduct.setEnterpriseName(productRequest.getEnterpriseName());
        //根据产品名称查找银行行号和id以及产品的贷款额度
        QueryWrapper<FinancialProduct> financialProductQueryWrapper = new QueryWrapper<>();
        financialProductQueryWrapper.eq("product_name",productRequest.getFkProductName());
        FinancialProduct financialProduct= financialProductMapper.selectOne(financialProductQueryWrapper);
        dealProduct.setBankId(financialProduct.getFkBankId());
        dealProduct.setBankName(financialProduct.getBankName());
        dealProduct.setAmountLoanLimit(financialProduct.getLoanLimit());

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String temp = dateFormat.format(date);
        dealProduct.setDealTime(dateFormat.parse(temp));
        System.out.println(dealProduct);

        //更新产品申请记录中的状态
        productRequest.setStatus(1);
        productRequest.setComment("交易成功");
        try {
            dealProductMapper.insert(dealProduct);
            productRequestMapper.updateById(productRequest);
        }catch (Exception e){
            responseResult.setSuccess(false);
            responseResult.setCode(BankCode.DEAL_FAILED.getCode());
            responseResult.setMessage(BankCode.DEAL_FAILED.getMessage());
            return responseResult;
        }

        responseResult.setMessage(BankCode.DEAL_SUCCESS.getMessage());
        responseResult.setSuccess(true);
        responseResult.setCode(BankCode.DEAL_SUCCESS.getCode());
        return responseResult;
    }
    /*
     * 企业申请拒绝
     */
    @Override
    public ResponseResult productApplyRefuse(int request_id) {
        ResponseResult responseResult = new ResponseResult();
        //根据id查出企业的产品申请

        try {
            QueryWrapper<ProductRequest> productRequestQueryWrapper = new QueryWrapper<>();
        productRequestQueryWrapper.eq("request_id",request_id);
        ProductRequest productRequest= productRequestMapper.selectOne(productRequestQueryWrapper);
        productRequest.setStatus(2);
        productRequest.setComment("产品申请已被拒绝");

            productRequestMapper.updateById(productRequest);
        }catch (Exception e){
            responseResult.setSuccess(false);
            responseResult.setCode(BankCode.REFUSE_FAILED.getCode());
            responseResult.setMessage(BankCode.REFUSE_FAILED.getMessage());
            return responseResult;
        }
        responseResult.setMessage(BankCode.REFUSE_SUCCESS.getMessage());
        responseResult.setSuccess(true);
        responseResult.setCode(BankCode.REFUSE_SUCCESS.getCode());
        return responseResult;
    }

    /*
     * 修改产品申请进度
     */
    @Override
    public ResponseResult modifyApplySchedule(int request_id, String Comment) {
        ResponseResult responseResult = new ResponseResult();
        try {
        QueryWrapper<ProductRequest> productRequestQueryWrapper = new QueryWrapper<>();
        productRequestQueryWrapper.eq("request_id",request_id);
        ProductRequest productRequest= productRequestMapper.selectOne(productRequestQueryWrapper);
        productRequest.setComment(Comment);

        productRequestMapper.updateById(productRequest);
        }catch (Exception e){
            responseResult.setSuccess(false);
            responseResult.setCode(BankCode.MODIFY_FAILED.getCode());
            responseResult.setMessage(BankCode.MODIFY_FAILED.getMessage());
            return responseResult;
        }
        responseResult.setMessage(BankCode.MODIFY_SUCCESS.getMessage());
        responseResult.setSuccess(true);
        responseResult.setCode(BankCode.MODIFY_SUCCESS.getCode());
        return responseResult;
    }

}
